package top.linruchang.bean;

import cn.hutool.core.util.ObjectUtil;
import com.sun.istack.internal.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import top.linruchang.enums.RegexContentEnum;
import top.linruchang.util.RegexUtil;
import top.linruchang.util.replace.ReplaceInterface;
import top.linruchang.util.translate.YouDaoYunTranslate;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 作用：正则表达式与替换字符串对象
 *
 * @author LinRuChang
 * @version 1.0
 * @date 2020/11/02
 * @since 1.8
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
@Accessors(chain = true)
public class ReplaceStrAndRegexStr {

    /**正则匹配到的字符串需要被替换的字符串*/
    String replaceStr;

    /**如果匹配的字符需要经过某种工具进行格式化 - 而不是固定的字符串*/
    ReplaceInterface replaceInterface;

    /**可参考内置的正则规则{@link RegexContentEnum}*/
    Collection<String> regexs;


    public ReplaceStrAndRegexStr(String replaceStr, Collection<String> regexs) {
        this.replaceStr = replaceStr;
        this.regexs = regexs;
    }

    /**
     * 添加内容正则 - 链式编程
     * @param regex
     * @return
     */
    public ReplaceStrAndRegexStr addRegex(@NotNull String regex) {
        if(ObjectUtil.isEmpty(regexs)) {
            this.regexs = new ArrayList<>();
        }
        regexs.add(regex);
        return this;
    }

    /**
     * <p>
     * description: 将源内容通过正则进行处理
     * </p>
     *
     * @param sourceContent
     * @return java.lang.String 处理后的内容
     */
    public String processContent(String sourceContent) {
        return RegexUtil.replaceByRegexs(sourceContent, this);
    }



    /**==============================静态工厂==========================================*/

    /**
     * <p>
     * description: 将Map<替换字符，多个正则>转成ReplaceStrAndRegexStr类型容器对象 【Key替换字符、Value被替换字符正则】
     * </p>
     *
     * @param rules
     * @return java.util.Collection<top.linruchang.bean.ReplaceStrAndRegexStr>
     */
    public static Collection<ReplaceStrAndRegexStr> toReplaceStrAndRegexStrs(Map<String, Collection<String>> rules) {
        return rules.entrySet().stream()
                .map(entry -> new ReplaceStrAndRegexStr(entry.getKey(), entry.getValue()))
                .collect(Collectors.toSet());
    }


    public static List<ReplaceStrAndRegexStr> defaultContentRule(boolean isTranslate) {
        List<ReplaceStrAndRegexStr> rsarss = new ArrayList<>();
        if(isTranslate) {
            ReplaceStrAndRegexStr defaultContentRuleButNotChinese = defaultContentRule();
            defaultContentRuleButNotChinese.getRegexs().remove(RegexContentEnum.CHINESE.getRegex());
            rsarss.add(defaultContentRuleButNotChinese);
            rsarss.add(chineseToEnglishContentRule());
        }else {
            rsarss.add(defaultContentRule());
        }
        return rsarss;
    }

    /**
     * <p>
     * description: 默认使用{@link RegexContentEnum}下的所有正则进行匹配内容
     * </p>
     *
     * @param replaceStr 正则匹配到的字符串需要使用什么进行替换
     * @return ReplaceStrAndRegexStr
     */
    public static ReplaceStrAndRegexStr defaultContentRule(String replaceStr) {
        return new ReplaceStrAndRegexStr().setRegexs(RegexContentEnum.getEnumAllRegex()).setReplaceStr(replaceStr);
    }

    /**
     * 默认正则匹配的东西全部置空
     * @return
     */
    public static ReplaceStrAndRegexStr defaultContentRule() {
        return new ReplaceStrAndRegexStr().setRegexs(RegexContentEnum.getEnumAllRegex()).setReplaceStr("");
    }

    /**
     * 匹配汉字并将其翻译
     * @return
     */
    public static ReplaceStrAndRegexStr chineseToEnglishContentRule() {
        YouDaoYunTranslate youDaoYunTranslate = new YouDaoYunTranslate();
        ReplaceStrAndRegexStr replaceStrAndRegexStr = new ReplaceStrAndRegexStr();
        replaceStrAndRegexStr.setRegexs(Arrays.asList(RegexContentEnum.CHINESE.getRegex()));
        replaceStrAndRegexStr.setReplaceInterface((String source) -> {
            return youDaoYunTranslate.translate(source);
        });
        return replaceStrAndRegexStr;
    }

}
